﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Configuration;

namespace FileOperationWinform
{
    public partial class FrmExceDetail : Form
    {

        public FrmExceDetail()
        {
            InitializeComponent();
        }
       
        private int edit = 0;
        private string s;
        private string s1;
        private int index;
        public FrmExceDetail(string s,string s1,int x)
        {
            InitializeComponent();
            this.edit = 1;
            this.s = s;
            this.s1 = s1;
            this.index = x;
        }
        private void FrmExceDetail_Load(object sender, EventArgs e)
        {
            FileOpera ft = new FileOpera();
            string st = ft.SkinLoad();
            if (!(string.Equals(st, "Error")))
            {
                skinEngine1.SkinFile = System.IO.Path.Combine(Application.StartupPath, st);
            }
            if (this.edit!=1)
            {
                this.textBox1.Text = Convert.ToString(form.listView1.Items.Count+1); ;
            }
            else
            {
                this.textBox1.Text = s;
                this.textBox2.Text = s1;
            }
        }

        public FrmExcept form = null;
        public void GetForm(FrmExcept theform)
        {
            form = theform;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string st=textBox2.Text.Trim();
            if (st=="")
            {
                MessageBox.Show("格式不能指定为空！\r\n");
                return;
            }
            if (!(st == ".zip" || st == ".rar" || st == ".pdf" ||
                st == ".doc" || st == ".ppt" || st == ".xls" ||
                st == ".docx" || st == ".pptx" || st == ".xlsx"))
            {
                if (this.edit != 1)
                {
                    FileOpera fs = new FileOpera();
                    if (fs.SerorceFile(st)==1)
                    {
                        errorProvider1.Clear();
                        errorProvider1.SetError(textBox2, "该格式已经指定,不能重复指定！");
                        return;
                    }
                    if (file == false)
                    {

                        ListViewItem first = new ListViewItem();
                        string str = Convert.ToString(form.listView1.Items.Count + 1);
                        first.SubItems[0].Text = str;
                        first.SubItems.Add(textBox2.Text.Trim());
                        form.listView1.Items.Add(first);

                        foreach (Control ctr1 in this.form.Controls["panel1"].Controls)
                        {
                            string s = ctr1.Name;
                        }

                        //form.Controls[System.Windows.Forms.Label].Text = "ddd";
                        //form.Activate();
                        ////form.reshform();
                        //form.Controls["label1"].Focus();
                        //form.Controls["label1"].Text = "ddd";

                        //this.ParentForm.Controls["label1"].Focus();
                        //this.ParentForm.Controls["label1"].Text = "ddd";
                        // form.FindForm().Focus();
                        fs.FileAdd(form.listView1.Items.Count - 1, str, textBox2.Text.Trim());
                    }
                }
                else
                {
                    FileOpera fs = new FileOpera();
                    ListViewItem first = new ListViewItem();
                    first.SubItems[0].Text = s;
                    first.SubItems.Add(textBox2.Text.Trim());
                    form.listView1.Items[index].Remove();
                    if (fs.SerorceFile(st) == 1)
                    {
                        errorProvider1.Clear();
                        errorProvider1.SetError(textBox2, "该格式已经指定,不能重复指定！");
                        return;
                    }
                    if (file == false)
                    {

                        form.listView1.Items.Add(first);
                        //form.Activate();
                        //this.ParentForm.Activate();
                        //this.ParentForm.Controls["label1"].Text = "ddd";
                        //this.ParentForm.Controls["btnExceptAdd"].FindForm().Focus();
                        form.Controls["label1"].Text = "ddd";
                        form.Controls["label1"].Focus();
                        //this.MdiParent.Controls["label1"].Text = "ddd";
                        //this.MdiParent.Controls["label1"].Focus();
                        fs.updateFile(index, s, textBox2.Text.Trim());
                    }
                }
            }
            else
            {
                errorProvider1.Clear();
                errorProvider1.SetError(textBox2, "该格式已经存在,无需指定！");
                return;
            }
            this.Close();
           // form.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        bool file = false;
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if (this.textBox2.Text.Trim().Contains('.')==false)
            {
                file = true;
                errorProvider1.SetError(textBox2, "该格式已不正确！");
                return;
            }
            file = false;
        }

        private void FrmExceDetail_KeyPress(object sender, KeyPressEventArgs e)
        {
          
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                button1_Click(button1, null);

            }
        }
    }
}
